﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IService;
using MyBlog.Model;
using MyBlog.WebApi.ApiResult;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class BlogNewsController : ControllerBase
    {
        private readonly IBlogNewsService _iBlogNewsService;
        public BlogNewsController(IBlogNewsService iBlogNewsService)
        {
            this._iBlogNewsService = iBlogNewsService;
        }

        [HttpGet("BlogNews")]
        public async Task<ActionResult<ApiResults>> GetBlogNews()
        {
            int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            var data =  await _iBlogNewsService.QueryAsync(c=>c.WriterId == id);
            if (data.Count == 0)
            {
                return ApiResultsHelper.Error(204, "未查询到数据,数据表为空");
            }
            return ApiResultsHelper.Success("查询成功", data);
        }

        [HttpPost("Create")]
        public async Task<ActionResult<ApiResults>> Create(string title,string content,int typeid)
        {
            BlogNews blogNews = new BlogNews()
            {
                Title = title,
                Content = content,
                Time = DateTime.Now,
                TypeId = typeid,
                BrowseCount = 0,
                LikeCount = 0,
                WriterId = Convert.ToInt32(this.User.FindFirst("Id").Value)
            };
            bool res = await _iBlogNewsService.CreateAsync(blogNews);
            if (!res)
            {
                return ApiResultsHelper.Error(500,"创建数据失败");
            }
            return ApiResultsHelper.Success(201, "创建数据成功");
        }

        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResults>> Delete(int id)
        {
            
            bool res = await _iBlogNewsService.DeleteAsync(id);
            if (!res)
            {
                return ApiResultsHelper.Error(500,"删除数据失败");
            }
            return ApiResultsHelper.Success("删除数据成功");
        }

        [HttpPut("Edit")]
        public async Task<ActionResult<ApiResults>> Edit(int id,string title,string content,int typeid)
        {
            var blogNews =await _iBlogNewsService.FindAsync(id);
            if (blogNews == null)
            {
                return ApiResultsHelper.Error(204,"找不到该文章");
            }
            blogNews.Title = title;
            blogNews.Content = content;
            blogNews.TypeId = typeid;
            bool res = await _iBlogNewsService.EditAsync(blogNews);
            if (!res)
            {
                return ApiResultsHelper.Error(500,"修改数据失败");
            }
            return ApiResultsHelper.Success("修改数据成功");
        }


        [HttpGet("BlogNewsPage")]
        public async Task<ApiResults> GetBlogNewsPage([FromServices] IMapper iMapper, int page, int size)
        {
            RefAsync<int> total = 0;
            var blognews = await _iBlogNewsService.QueryAsync(page, size, total);
            try
            {
                var blognewsDTO = iMapper.Map<List<BlogNewsDTO>>(blognews);
                return ApiResultsHelper.Success(blognewsDTO, total);
            }
            catch (Exception)
            {
                return ApiResultsHelper.Error("AutoMapper映射错误");
            }
        }
    }
}
